package wblog.service;

import com.google.inject.Inject;
import com.google.inject.servlet.RequestScoped;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestScoped
public class LoginUserService {

    private HttpServletRequest request;

    @Inject
    public LoginUserService(HttpServletRequest request) {
        this.request = request;
    }

    public String getLoginUserId() {
        HttpSession session = request.getSession(false);
        if (session == null) return null;
        return (String) session.getAttribute("loginUser");
    }

    public void setLoginUserId(String id) {
        if (id == null) {
            HttpSession session = request.getSession(false);
            if (session == null) return;
            session.invalidate();
        } else {
            request.getSession().setAttribute("loginUser", id);
        }
    }
}
